
Imports std
Imports System.IO

Module grepW

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim reader As TextReader = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim i As Int32
        Dim count As Int32 = 0
        Dim line As Int32 = 0
        Dim needline As Boolean = False
        Dim optionCount As Int32 = 0

        debugpause()

        If args.Length < 2 Then
            raiseError("argument count error, [/l] input output grepString0 grepString1 ... grepStringN, " + _
                        "/l means output line number, if output is '/c', output count, if input or output is '/-', use console. " + _
                        "program will transform backslash.", errorHandle.errorType.critical)
        Else
            optionCount = 0
            If strsame(args(optionCount), "/l", False) Then
                optionCount += 1
                needline = True
            End If

            If args(optionCount) = "/-" Then
                reader = Console.In
            Else
                reader = New StreamReader(New FileStream(args(optionCount), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            End If
            assert(Not reader Is Nothing, "file no found, " + args(optionCount))

            optionCount += 1
            If args(optionCount) = "/-" Then
                writer = Console.Out
            ElseIf args(optionCount) <> "/c" Then
                writer = New StreamWriter(New FileStream(args(optionCount), FileMode.Create, FileAccess.Write, FileShare.Read))
                assert(Not writer Is Nothing, "file no found, " + args(optionCount))
            End If

            optionCount += 1
            For i = optionCount To args.Length() - 1
                unescape(args(i))
            Next

            input = reader.ReadLine()
            While Not input Is Nothing
                If Not isemptystring(input) Then
                    For i = optionCount To args.Length - 1
                        If input.Contains(args(i)) Then
                            Exit For
                        End If
                    Next

                    If i < args.Length Then
                        If writer Is Nothing Then
                            count += 1
                        Else
                            If needline Then
                                writer.WriteLine(input + character.tab + Convert.ToString(line))
                            Else
                                writer.WriteLine(input)
                            End If
                        End If
                    End If
                End If
                input = reader.ReadLine
                line += 1
            End While

            If writer Is Nothing Then
                Console.WriteLine(count)
            End If
            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
